package com.mission.schedule.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.text.TextUtils;

import com.android.volley.AuthFailureError;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request.Method;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.mission.schedule.activity.NewFriendsActivity;
import com.mission.schedule.add603.bean.UpNew;
import com.mission.schedule.applcation.App;
import com.mission.schedule.bean.CalenderNoteBean;
import com.mission.schedule.bean.CalenderNoteSynBean;
import com.mission.schedule.bean.SuccessOrFailBean;
import com.mission.schedule.bean.qd606.NoteTitleDetailBean;
import com.mission.schedule.constants.ShareFile;
import com.mission.schedule.constants.URLConstants;
import com.mission.schedule.cutimage.Log;
import com.mission.schedule.entity.CLRepeatTable;
import com.mission.schedule.entity.ScheduleTable;
import com.mission.schedule.utils.SharedPrefUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 本地同步数据操作，同步日程 日程分类，重复，本地联系人，清单标题，清单详情，清单分类
 * Created by liwei on 2017/5/18.
 */

public class LocalSynService extends Service {

    App application = App.getDBcApplication();
    SharedPrefUtil sharedPrefUtil = null;
    String UserId;
    Intent localSynServiceReciver;
    List<Map<String, String>> upRepeatList, upList;
    List<NoteTitleDetailBean.TDelListBean> tDelListBeanList;
    List<NoteTitleDetailBean.ListBean> listBeanList;
    List<NewFriendsActivity.newFriendBean.PageBean.ItemsBean> itemsBeanList;
    List<CalenderNoteBean.PageBean.ItemsBean> calenderBeanlist;
    List<CalenderNoteBean.PageBean.ItemsBean> calenderBeanlist1 = new ArrayList<>();
    List<CalenderNoteBean.PageBean.ItemsBean> repeatBeanlist = new ArrayList<>();
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        sharedPrefUtil = new SharedPrefUtil(getApplication(),
                ShareFile.USERFILE);
        UserId = sharedPrefUtil.getString(getApplicationContext(),
                ShareFile.USERFILE, ShareFile.USERID, "");
        //判断是不是最小化同步
        if (sharedPrefUtil.getString(getApplication(),
                ShareFile.USERFILE, ShareFile.ISNEWYOUKE, "0").equals("-1")) {
            try {
                upRepeatList = application.QueryAllChongFuData(2);
                upList = application.queryAllSchData(-1, 0, 0);
                tDelListBeanList = application.getAllNoteTitlesData(UserId, true);
                listBeanList = application.getAllNoteDetailData(UserId, true);
                itemsBeanList = application.getAllNewFriendData(UserId, true);
                calenderBeanlist = application.getAllCalenderNoteDetailData(UserId, true);
                for (CalenderNoteBean.PageBean.ItemsBean bean: calenderBeanlist) {
                    if (bean.titleId.substring(0,2).equals("99")) {
                        repeatBeanlist.add(bean);
                    } else {
                        calenderBeanlist1.add(bean);
                    }
                }
                if (intent != null && intent.getBooleanExtra("isMinLocal", false)) {
                    Log.e("TAG", "TimeSheet App uploading local data");
                    if (upList != null && upList.size() > 0) {
                        uploadCalenderCode(true);
                    }
                    if (upRepeatList != null && upRepeatList.size() > 0) {
                        uploadRepeatCode(true);
                    }
                    if (calenderBeanlist1 != null && calenderBeanlist1.size() > 0) {
                        uploadCalendernote();
                    }
                    if (repeatBeanlist != null && repeatBeanlist.size() > 0) {
                        uploadRepeatnote();
                    }
                    if ((listBeanList != null && listBeanList.size() > 0) || tDelListBeanList != null && tDelListBeanList.size() > 0) {
                        uploadNoteCode(true);
                        uploadGongKaiHongdian(tDelListBeanList);
                    }
                    if (itemsBeanList != null && itemsBeanList.size() > 0) {
                        uploadLocalFriends(true);
                    }
                } else {
                    uploadCalender();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return super.onStartCommand(intent, flags, startId);
    }

    /**
     * 上传日程数据
     */
    private void uploadCalender() {
        if (upList == null || upList.size() == 0) {
            uploadCalenderStatus(false, false);
        } else {
            uploadCalenderCode(false);
        }
    }

    private void uploadCalenderCode(final boolean isMinLocal) {
        try {
            JSONArray jsonarray = new JSONArray();
            JSONObject jsonobject = new JSONObject();
            if (upList != null && upList.size() > 0) {
                for (int i = 0; i < upList.size(); i++) {
//                    if ((Integer.parseInt(upList.get(i).get(ScheduleTable.schID)) < 0
//                            && Integer.parseInt(upList.get(i).get(ScheduleTable.schUpdateState)) == 1)
//                            || (Integer.parseInt(upList.get(i).get(ScheduleTable.schID)) > 0
//                            && Integer.parseInt(upList.get(i).get(ScheduleTable.schUpdateState)) != 1)) {
                        JSONObject jsonobject1 = new JSONObject();
                        jsonobject1.put("tempId", upList.get(i).get(ScheduleTable.schID));
                        jsonobject1.put("cId", upList.get(i).get(ScheduleTable.schID));
                        jsonobject1.put("cRecommendId", upList.get(i).get(ScheduleTable.schcRecommendId));
                        jsonobject1.put("cUid", UserId);
                        jsonobject1.put("cContent", upList.get(i).get(ScheduleTable.schContent));
                        jsonobject1.put("cDate", upList.get(i).get(ScheduleTable.schDate));
                        jsonobject1.put("cTime", upList.get(i).get(ScheduleTable.schTime));
                        jsonobject1.put("cIsAlarm", upList.get(i).get(ScheduleTable.schIsAlarm));
                        jsonobject1.put("cBeforTime", upList.get(i).get(ScheduleTable.schBeforeTime));
                        jsonobject1.put("cDisplayAlarm", upList.get(i).get(ScheduleTable.schDisplayTime));
                        jsonobject1.put("cPostpone", upList.get(i).get(ScheduleTable.schIsPostpone));
                        jsonobject1.put("cImportant", upList.get(i).get(ScheduleTable.schIsImportant));
                        jsonobject1.put("cColorType", upList.get(i).get(ScheduleTable.schColorType));
                        jsonobject1.put("cIsEnd", upList.get(i).get(ScheduleTable.schIsEnd));
                        jsonobject1.put("cCreateTime", upList.get(i).get(ScheduleTable.schCreateTime));
                        jsonobject1.put("cTags", upList.get(i).get(ScheduleTable.schTags));
                        jsonobject1.put("cType", upList.get(i).get(ScheduleTable.schSourceType));
                        jsonobject1.put("cTypeDesc", upList.get(i).get(ScheduleTable.schSourceDesc));
                        jsonobject1.put("cTypeSpare", upList.get(i).get(ScheduleTable.schSourceDescSpare));
                        jsonobject1.put("cRepeatId", upList.get(i).get(ScheduleTable.schRepeatID));
                        jsonobject1.put("cRepeatDate", upList.get(i).get(ScheduleTable.schRepeatDate));
                        jsonobject1.put("cUpdateTime", upList.get(i).get(ScheduleTable.schUpdateTime));
                        jsonobject1.put("cOpenState", upList.get(i).get(ScheduleTable.schOpenState));
                        jsonobject1.put("cRecommendName", upList.get(i).get(ScheduleTable.schcRecommendName));
                        jsonobject1.put("updateState", upList.get(i).get(ScheduleTable.schUpdateState));
                        jsonobject1.put("cAlarmSoundDesc", upList.get(i).get(ScheduleTable.schRingDesc));
                        jsonobject1.put("cAlarmSound", upList.get(i).get(ScheduleTable.schRingCode));
                        jsonobject1.put("schRead", upList.get(i).get(ScheduleTable.schRead));
                        jsonobject1.put("attentionid", upList.get(i).get(ScheduleTable.schAID));
                        jsonobject1.put("aType", upList.get(i).get(ScheduleTable.schAType));
                        jsonobject1.put("webUrl", upList.get(i).get(ScheduleTable.schWebURL));
                        jsonobject1.put("imgPath", upList.get(i).get(ScheduleTable.schImagePath));
                        jsonobject1.put("cSchRepeatLink", upList.get(i).get(ScheduleTable.schRepeatLink));
                        jsonobject1.put("pIsEnd", upList.get(i).get(ScheduleTable.schIsEnd));
                        jsonobject1.put("remark", upList.get(i).get(ScheduleTable.schRemark1));
                        jsonobject1.put("remark1", upList.get(i).get(ScheduleTable.schRemark2));
                        jsonobject1.put("remark2", upList.get(i).get(ScheduleTable.schRemark3));
                        jsonobject1.put("remark4", upList.get(i).get(ScheduleTable.schRemark4));
                        jsonobject1.put("remark5", upList.get(i).get(ScheduleTable.schRemark5));
                        jsonobject1.put("remark7", upList.get(i).get(ScheduleTable.schRemark7));
                        jsonobject1.put("remark10", upList.get(i).get(ScheduleTable.schRemark10));
                        jsonobject1.put("remark11", upList.get(i).get(ScheduleTable.schRemark11));
                        jsonobject1.put("remark12", upList.get(i).get(ScheduleTable.schRemark12));
                        jsonarray.put(jsonobject1);
                    }
//                }
            }
            jsonobject.put("", jsonarray);
            final String schjson = jsonobject.toString();
            String schuppath = "";
            if (sharedPrefUtil.getString(getApplicationContext(), ShareFile.USERFILE, ShareFile.UPLOADTYPE, "0").equals("1")) {
                schuppath = URLConstants.同步数据接口新;
            } else if (sharedPrefUtil.getString(getApplicationContext(), ShareFile.USERFILE, ShareFile.UPLOADTYPE, "0").equals("0")) {
                schuppath = URLConstants.同步数据接口2新;
            }

            StringRequest request = new StringRequest(Method.POST, schuppath,
                    new Response.Listener<String>() {

                        @Override
                        public void onResponse(final String result) {
                            if (!TextUtils.isEmpty(result)) {
                                Gson gson = new Gson();
                                UpNew UpNew = gson.fromJson(result, UpNew.class);
                                List<UpNew.ListBean> listBeanList;
                                if (UpNew.status == 0) {
                                    listBeanList = UpNew.list;
                                    if (listBeanList != null) {
                                        if (listBeanList.size() > 0) {
                                            for (int i = 0; i < listBeanList.size(); i++) {
                                                if (listBeanList.get(i).dataState == 1) {//新增
                                                    //新增成功就修改id和状态
                                                    if (listBeanList.get(i).state == 0) {//成功
                                                        application.UpdateSchID(Integer.parseInt(listBeanList.get(i).id), listBeanList.get(i).calendId);
                                                        application.updateUpdateState(listBeanList.get(i).calendId);
                                                        application.UpdateClockSchID(listBeanList.get(i).id, listBeanList.get(i).calendId + "");
                                                    } else if (listBeanList.get(i).state == 1) {//失败
                                                        Log.e("UPLOADCALENDER", listBeanList.get(i).calendId + ":FAILED");
                                                        application.updateUpdateStateFaild(listBeanList.get(i).calendId, 1);
                                                    } else if (listBeanList.get(i).state == 2) {
                                                        application.updateUpdateStateFaild(listBeanList.get(i).calendId, -1);
                                                    }
                                                } else if (listBeanList.get(i).dataState == 3) {//删除
                                                    //删除成功删除数据
                                                    if (listBeanList.get(i).state == 0) {
                                                        application.deleteScheduleData(listBeanList.get(i).calendId);
                                                    } else if (listBeanList.get(i).state == 1) {
                                                        Log.e("UPLOADCALENDER", listBeanList.get(i).calendId + ":FAILED");
                                                        application.updateUpdateStateFaild(listBeanList.get(i).calendId, 3);
                                                    } else if (listBeanList.get(i).state == 2) {
                                                        application.updateUpdateStateFaild(listBeanList.get(i).calendId, -1);
                                                    }
                                                    application.deleteSch(listBeanList.get(i).calendId);
                                                } else if (listBeanList.get(i).dataState == 2) {//修改
                                                    //修改成功修改状态
                                                    if (listBeanList.get(i).state == 0) {
                                                        application.updateUpdateState(listBeanList.get(i).calendId);
                                                    } else if (listBeanList.get(i).state == 1) {
                                                        Log.e("UPLOADCALENDER", listBeanList.get(i).calendId + ":FAILED");
                                                        application.updateUpdateStateFaild(listBeanList.get(i).calendId, 2);
                                                    } else if (listBeanList.get(i).state == 2) {
                                                        application.updateUpdateStateFaild(listBeanList.get(i).calendId, -1);
                                                    }
                                                }
                                                sharedPrefUtil.putString(
                                                        getApplication(), ShareFile.USERFILE, ShareFile.DOWNREPTIME,
                                                        listBeanList.get(i).downTime.replace("T", " "));
                                            }
                                        }
                                    }
                                    uploadCalenderStatus(false, isMinLocal);
                                } else {
                                    uploadCalenderStatus(true, isMinLocal);
                                }
                            } else {
                                uploadCalenderStatus(true, isMinLocal);
                            }
                        }
                    }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError volleyError) {
                    uploadCalenderStatus(true, isMinLocal);
                }
            }) {
                @Override
                protected Map<String, String> getParams() throws AuthFailureError {
                    Map<String, String> map = new HashMap<String, String>();
                    map.put("json", schjson);
                    return map;
                }
            };
            request.setTag("uploadCalender");
            request.setRetryPolicy(new DefaultRetryPolicy(5000, 1, 1.0f));
            App.getHttpQueues().add(request);

        } catch (Exception e) {
            e.printStackTrace();
            uploadCalenderStatus(true, isMinLocal);
        }
    }

    /**
     * 上传日程数据状态
     *
     * @param isFailed true 失败 false 成功
     */
    private void uploadCalenderStatus(boolean isFailed, boolean isMinLocal) {
        if (!isMinLocal) {
            localSynServiceReciver = new Intent("localSynServiceReciver");
            localSynServiceReciver.putExtra("cflag", isFailed ? "cflag_false" : "cflag");
            sendBroadcast(localSynServiceReciver);
            uploadRepeat();
        }
    }

    /**
     * 上传重复数据
     */
    private void uploadRepeat() {
        if (upRepeatList == null || upRepeatList.size() == 0) {
            uploadRepeatStatus(false, false);
        } else {
            uploadRepeatCode(false);
        }
    }

    private void uploadRepeatCode(final boolean isMinlocal) {
        try {
            JSONArray jsonarray1 = new JSONArray();
            JSONObject jsonobject2 = new JSONObject();
            for (int i = 0; i < upRepeatList.size(); i++) {
                if ((Integer.parseInt(upRepeatList.get(i).get(CLRepeatTable.repID)) < 0
                        && Integer.parseInt(upRepeatList.get(i).get(CLRepeatTable.repUpdateState)) == 1)
                        || (Integer.parseInt(upRepeatList.get(i).get(CLRepeatTable.repID)) > 0
                        && Integer.parseInt(upRepeatList.get(i).get(CLRepeatTable.repUpdateState)) != 1)) {
                    JSONObject jsonobject3 = new JSONObject();
                    if (Integer.parseInt(upRepeatList.get(i).get(CLRepeatTable.repID)) < 0) {
                        jsonobject3.put("tempId", upRepeatList.get(i).get(CLRepeatTable.repID));
                    } else {
                        jsonobject3.put("repId", upRepeatList.get(i).get(CLRepeatTable.repID));
                    }
                    jsonobject3.put("repUid", UserId);
                    jsonobject3.put("repBeforeTime", upRepeatList.get(i).get(CLRepeatTable.repBeforeTime));
                    jsonobject3.put("repColorType", upRepeatList.get(i).get(CLRepeatTable.repColorType));
                    jsonobject3.put("repDisplayTime", upRepeatList.get(i).get(CLRepeatTable.repDisplayTime));
                    jsonobject3.put("repType", upRepeatList.get(i).get(CLRepeatTable.repType));
                    jsonobject3.put("repIsAlarm", upRepeatList.get(i).get(CLRepeatTable.repIsAlarm));
                    jsonobject3.put("repIsPuase", upRepeatList.get(i).get(CLRepeatTable.repIsPuase));
                    jsonobject3.put("repIsImportant", upRepeatList.get(i).get(CLRepeatTable.repIsImportant));
                    jsonobject3.put("repSourceType", upRepeatList.get(i).get(CLRepeatTable.repSourceType));
                    jsonobject3.put("repOpenState", upRepeatList.get(i).get(CLRepeatTable.repOpenState));
                    jsonobject3.put("repstateone", upRepeatList.get(i).get(CLRepeatTable.repStateOne));
                    jsonobject3.put("repstatetwo", upRepeatList.get(i).get(CLRepeatTable.repStateTwo));
                    jsonobject3.put("recommendedUserId", upRepeatList.get(i).get(CLRepeatTable.repcommendedUserId));
                    jsonobject3.put("repTypeParameter", upRepeatList.get(i).get(CLRepeatTable.repTypeParameter));
                    jsonobject3.put("repStartDate", upRepeatList.get(i).get(CLRepeatTable.repStartDate));
                    jsonobject3.put("repNextCreatedTime", upRepeatList.get(i).get(CLRepeatTable.repNextCreatedTime));
                    jsonobject3.put("repLastCreatedTime", upRepeatList.get(i).get(CLRepeatTable.repLastCreatedTime));
                    jsonobject3.put("repInitialCreatedTime", upRepeatList.get(i).get(CLRepeatTable.repInitialCreatedTime));
                    jsonobject3.put("repContent", upRepeatList.get(i).get(CLRepeatTable.repContent));
                    jsonobject3.put("repCreateTime", upRepeatList.get(i).get(CLRepeatTable.repCreateTime));
                    jsonobject3.put("repChangeState", upRepeatList.get(i).get(CLRepeatTable.repUpdateState));
                    jsonobject3.put("repSourceDesc", upRepeatList.get(i).get(CLRepeatTable.repSourceDesc));
                    jsonobject3.put("repSourceDescSpare", upRepeatList.get(i).get(CLRepeatTable.repSourceDescSpare));
                    jsonobject3.put("repTime", upRepeatList.get(i).get(CLRepeatTable.repTime));
                    jsonobject3.put("repRingDesc", upRepeatList.get(i).get(CLRepeatTable.repRingDesc));
                    jsonobject3.put("repRingCode", upRepeatList.get(i).get(CLRepeatTable.repRingCode));
                    jsonobject3.put("repUpdateTime", upRepeatList.get(i).get(CLRepeatTable.repUpdateTime));
                    jsonobject3.put("recommendedUserName", upRepeatList.get(i).get(CLRepeatTable.repcommendedUserName));
                    jsonobject3.put("repdateone", upRepeatList.get(i).get(CLRepeatTable.repDateOne));
                    jsonobject3.put("repdatetwo", upRepeatList.get(i).get(CLRepeatTable.repDateTwo));
                    jsonobject3.put("aType", upRepeatList.get(i).get(CLRepeatTable.repAType));
                    jsonobject3.put("webUrl", upRepeatList.get(i).get(CLRepeatTable.repWebURL));
                    jsonobject3.put("imgPath", upRepeatList.get(i).get(CLRepeatTable.repImagePath));
                    jsonobject3.put("repInSTable", upRepeatList.get(i).get(CLRepeatTable.repInSTable));
                    jsonobject3.put("repEndState", upRepeatList.get(i).get(CLRepeatTable.repEndState));
                    jsonobject3.put("parReamrk", upRepeatList.get(i).get(CLRepeatTable.parReamrk));
                    jsonobject3.put("repRead", upRepeatList.get(i).get(CLRepeatTable.repRead));
                    jsonobject3.put("recommendedUserId", upRepeatList.get(i).get(CLRepeatTable.repcommendedUserId));
                    jsonobject3.put("dataId", upRepeatList.get(i).get(CLRepeatTable.dataId));
                    jsonobject3.put("dataType", upRepeatList.get(i).get(CLRepeatTable.dataType));
                    jsonobject3.put("remark1", upRepeatList.get(i).get(CLRepeatTable.remark1));
                    jsonobject3.put("remark2", upRepeatList.get(i).get(CLRepeatTable.remark2));
                    jsonobject3.put("remark3", upRepeatList.get(i).get(CLRepeatTable.remark3));
                    jsonobject3.put("remark4", upRepeatList.get(i).get(CLRepeatTable.remark4));
                    jsonobject3.put("remark5", upRepeatList.get(i).get(CLRepeatTable.remark5));
                    jsonarray1.put(jsonobject3);
                }
            }

            jsonobject2.put("data", jsonarray1);
            final String repJson = jsonobject2.toString();
            String repeatUpPath = URLConstants.重复数据上传新;
            StringRequest request = new StringRequest(Method.POST, repeatUpPath,
                    new Response.Listener<String>() {

                        @Override
                        public void onResponse(final String result) {
                            if (!TextUtils.isEmpty(result)) {
                                Gson gson = new Gson();
                                /**
                                 * 新重复同步改 2016-11-23 14:54
                                 */
                                UpNew UpNew = gson.fromJson(result, UpNew.class);
                                if (UpNew.status == 0) {
                                    List<UpNew.ListBean> listBeanList = UpNew.list;
                                    if (listBeanList.size() > 0) {
                                        for (int i = 0; i < listBeanList.size(); i++) {
                                            if (listBeanList.get(i).dataState == 1) {//新增
                                                if (listBeanList.get(i).state == 0) {//成功
                                                    application.updateRepUpdateState(listBeanList.get(i).calendId);
                                                } else if (listBeanList.get(i).state == 1 || listBeanList.get(i).state == 2) {//失败
                                                    Log.e("UPLOADREPEAT", listBeanList.get(i).calendId + ":FAILED");
                                                    application.updateRepUpdateStateFaild(listBeanList.get(i).calendId);
                                                }
                                                application.UpdateRepeatID(Integer.parseInt(listBeanList.get(i).id), listBeanList.get(i).calendId);
                                                application.UpdateSchschRepeatID(Integer.parseInt(listBeanList.get(i).id), listBeanList.get(i).calendId);
                                                application.updateRepUpdateState(listBeanList.get(i).calendId);
                                                application.UpdateClockRepID(listBeanList.get(i).id, listBeanList.get(i).calendId + "");
//                                                application.UpdateSchrepID(Integer.parseInt(listBeanList.get(i).id), listBeanList.get(i).calendId);
                                            } else if (listBeanList.get(i).dataState == 3) {//删除
                                                if (listBeanList.get(i).state == 0) {
                                                    application.updateRepUpdateState(listBeanList.get(i).calendId);
                                                } else if (listBeanList.get(i).state == 1 || listBeanList.get(i).state == 2) {
                                                    Log.e("UPLOADREPEAT", listBeanList.get(i).calendId + ":FAILED");
                                                    application.updateRepUpdateStateFaild(listBeanList.get(i).calendId);
                                                }
                                                application.deleteRepeatData(listBeanList.get(i).calendId + "");
                                                application.deleteChildSch(listBeanList.get(i).calendId + "");
                                                application.deleteRep(listBeanList.get(i).calendId);
                                            } else if (listBeanList.get(i).dataState == 2) {//修改
                                                if (listBeanList.get(i).state == 0) {
                                                    application.updateRepUpdateState(listBeanList.get(i).calendId);
                                                } else if (listBeanList.get(i).state == 1 || listBeanList.get(i).state == 2) {
                                                    Log.e("UPLOADREPEAT", listBeanList.get(i).calendId + ":FAILED");
                                                    application.updateRepUpdateStateFaild(listBeanList.get(i).calendId);
                                                }
                                            }
                                            sharedPrefUtil.putString(
                                                    getApplication(),
                                                    ShareFile.USERFILE,
                                                    ShareFile.DOWNREPTIME,
                                                    listBeanList.get(i).downTime.replace("T", " "));
                                        }
                                    }
                                    uploadRepeatStatus(false, isMinlocal);
                                } else {
                                    uploadRepeatStatus(true, isMinlocal);
                                }
                            }
                        }
                    }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError volleyError) {
                    uploadRepeatStatus(true, isMinlocal);
                }
            }) {
                @Override
                protected Map<String, String> getParams() {
                    Map<String, String> map = new HashMap<>();
                    map.put("json", repJson);
                    return map;
                }
            };
            request.setTag("uploadRepeat");
            request.setRetryPolicy(new DefaultRetryPolicy(5000, 1, 1.0f));
            App.getHttpQueues().add(request);
        } catch (Exception e) {
            e.printStackTrace();
            uploadRepeatStatus(true, isMinlocal);
        }
    }


    /**
     * 上传重复数据状态
     */
    private void uploadRepeatStatus(boolean isFailed, boolean isMinLocal) {
        if (!isMinLocal) {
            localSynServiceReciver = new Intent("localSynServiceReciver");
            localSynServiceReciver.putExtra("rflag", isFailed ? "rflag_false" : "rflag");
            sendBroadcast(localSynServiceReciver);
            uploadNote();
        } else {
            uploadNote();
        }
    }

    /**
     * 上传清单数据(标题和详情)
     */
    private void uploadNote() {
        if ((tDelListBeanList == null || tDelListBeanList.size() == 0) && (listBeanList == null || listBeanList.size() == 0)) {
            uploadNoteStatus(false, false);
        } else {
            uploadNoteCode(false);
        }
    }

    private void uploadCalendernote() {
        JSONObject listjsonObject = new JSONObject();
        JSONArray listjsonArray = new JSONArray();
        try {
            for (CalenderNoteBean.PageBean.ItemsBean bean : calenderBeanlist1) {
                JSONObject js = new JSONObject();
                js.put("id", bean.id);
                js.put("titleId", bean.titleId);
                js.put("uid", bean.uid);
                js.put("titles", bean.titles);
                js.put("imgPath", bean.imgPath);
                js.put("imgUrl", bean.imgUrl);
                js.put("shareUrl", bean.shareUrl);
                js.put("contents", bean.contents);
                js.put("nums", bean.nums);
                js.put("style", bean.style);
                js.put("endstate", bean.endstate);
                js.put("ltype", bean.ltype);
                js.put("orderId", bean.orderId);
                js.put("createTime", bean.createTime);
                js.put("changeTime", bean.changeTime);
                js.put("cpath", bean.cpath);
                js.put("curl", bean.curl);
                js.put("sdesc", bean.sdesc);
                js.put("localIds", bean.localIds);
                js.put("remark", bean.remark);
                js.put("remark1", bean.remark1);
                js.put("remark2", bean.remark2);
                js.put("remark3", bean.remark3);
                js.put("remark4", bean.remark4);
                js.put("remark5", bean.remark5);
                js.put("remark6", bean.remark6);
                js.put("updateState", bean.id < 0 ? 1 : App.getDBcApplication().getCalenderNoteDetailState(bean.titleId, UserId, bean.id));
                listjsonArray.put(js);
            }
            if (calenderBeanlist1.size() > 0) {
                listjsonObject.put("timetable", listjsonArray);
            }


            HttpUtils httpUtils = new HttpUtils(5000);
            RequestParams params = new RequestParams();
            params.addBodyParameter("data", listjsonObject == null ? "" : listjsonObject.toString());
            httpUtils.send(HttpMethod.POST, URLConstants.日程清单同步数据, params,
                    new RequestCallBack<String>() {

                        @Override
                        public void onFailure(HttpException e, String msg) {
                        }

                        @Override
                        public void onSuccess(ResponseInfo<String> responseInfo) {
                            if (!TextUtils.isEmpty(responseInfo.result)) {
                                Gson gson = new Gson();
                                CalenderNoteSynBean bean = gson.fromJson(responseInfo.result, CalenderNoteSynBean.class);
                                if (bean.status == 0) {
                                    List<CalenderNoteSynBean.ListBean> listBeanList = bean.list;

                                    for (CalenderNoteSynBean.ListBean listBean : listBeanList) {
                                        if (listBean.dataState == 1) {//新增
                                            if (listBean.state == 0) {//成功
                                                //修改id和状态值
                                                App.getDBcApplication().updateCalenderNoteDetailId(listBean.id, listBean.calendId, UserId);
                                                App.getDBcApplication().updateCalenderNoteDetailState("", listBean.calendId, UserId, 0, true);
                                            } else if (listBean.state == 1) {//失败  可以不操作,但是避免意外
                                                //更改状态为1
                                                App.getDBcApplication().updateCalenderNoteDetailState("", listBean.id, UserId, 1, true);
                                            } else if (listBean.state == 2) { //异常
                                                App.getDBcApplication().updateCalenderNoteDetailState("", listBean.id, UserId, -1, true);
                                            }
                                        } else if (listBean.dataState == 2) {//修改
                                            if (listBean.state == 0) {//成功
                                                //修改该id状态值
                                                App.getDBcApplication().updateCalenderNoteDetailState("", listBean.id, UserId, 0, true);
                                            } else if (listBean.state == 1) {//失败
                                                //更改状态为2
                                                App.getDBcApplication().updateCalenderNoteDetailState("", listBean.id, UserId, 2, true);
                                            } else if (listBean.state == 2) { //异常
                                                App.getDBcApplication().updateCalenderNoteDetailState("", listBean.id, UserId, -1, true);
                                            }
                                        } else if (listBean.dataState == 3) {//删除
                                            if (listBean.state == 0) {//成功
                                                //删除该id的清单
                                                App.getDBcApplication().deleteCalenderNoteTitledetailData(listBean.id, UserId, true);
                                            } else if (listBean.state == 1) {//失败
                                                //更改状态为3
                                                App.getDBcApplication().updateCalenderNoteDetailState("", listBean.id, UserId, 3, true);
                                            } else if (listBean.state == 2) { //异常
                                                App.getDBcApplication().updateCalenderNoteDetailState("", listBean.id, UserId, -1, true);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void uploadRepeatnote() {
        JSONObject listjsonObject = new JSONObject();
        JSONArray listjsonArray = new JSONArray();
        try {
            for (CalenderNoteBean.PageBean.ItemsBean bean : repeatBeanlist) {
                JSONObject js = new JSONObject();
                js.put("id", bean.id);
                js.put("titleId", bean.titleId);
                js.put("uid", bean.uid);
                js.put("titles", bean.titles);
                js.put("imgPath", bean.imgPath);
                js.put("imgUrl", bean.imgUrl);
                js.put("shareUrl", bean.shareUrl);
                js.put("contents", bean.contents);
                js.put("nums", bean.nums);
                js.put("style", bean.style);
                js.put("endstate", bean.endstate);
                js.put("ltype", bean.ltype);
                js.put("orderId", bean.orderId);
                js.put("createTime", bean.createTime);
                js.put("changeTime", bean.changeTime);
                js.put("cpath", bean.cpath);
                js.put("curl", bean.curl);
                js.put("sdesc", bean.sdesc);
                js.put("localIds", bean.localIds);
                js.put("remark", bean.remark);
                js.put("remark1", bean.remark1);
                js.put("remark2", bean.remark2);
                js.put("remark3", bean.remark3);
                js.put("remark4", bean.remark4);
                js.put("remark5", bean.remark5);
                js.put("remark6", bean.remark6);
                js.put("updateState", bean.id < 0 ? 1 : App.getDBcApplication().getCalenderNoteDetailState(bean.titleId, UserId, bean.id));
                listjsonArray.put(js);
            }
            if (repeatBeanlist.size() > 0) {
                listjsonObject.put("timetable", listjsonArray);
            }


            HttpUtils httpUtils = new HttpUtils(5000);
            RequestParams params = new RequestParams();
            params.addBodyParameter("data", listjsonObject == null ? "" : listjsonObject.toString());
            httpUtils.send(HttpMethod.POST, URLConstants.重复清单同步数据, params,
                    new RequestCallBack<String>() {

                        @Override
                        public void onFailure(HttpException e, String msg) {
                        }

                        @Override
                        public void onSuccess(ResponseInfo<String> responseInfo) {
                            if (!TextUtils.isEmpty(responseInfo.result)) {
                                Gson gson = new Gson();
                                CalenderNoteSynBean bean = gson.fromJson(responseInfo.result, CalenderNoteSynBean.class);
                                if (bean.status == 0) {
                                    List<CalenderNoteSynBean.ListBean> listBeanList = bean.list;

                                    for (CalenderNoteSynBean.ListBean listBean : listBeanList) {
                                        if (listBean.dataState == 1) {//新增
                                            if (listBean.state == 0) {//成功
                                                //修改id和状态值
                                                App.getDBcApplication().updateCalenderNoteDetailId(listBean.id, listBean.calendId, UserId);
                                                App.getDBcApplication().updateCalenderNoteDetailState("", listBean.calendId, UserId, 0, true);
                                            } else if (listBean.state == 1) {//失败  可以不操作,但是避免意外
                                                //更改状态为1
                                                App.getDBcApplication().updateCalenderNoteDetailState("", listBean.id, UserId, 1, true);
                                            } else if (listBean.state == 2) { //异常
                                                App.getDBcApplication().updateCalenderNoteDetailState("", listBean.id, UserId, -1, true);
                                            }
                                        } else if (listBean.dataState == 2) {//修改
                                            if (listBean.state == 0) {//成功
                                                //修改该id状态值
                                                App.getDBcApplication().updateCalenderNoteDetailState("", listBean.id, UserId, 0, true);
                                            } else if (listBean.state == 1) {//失败
                                                //更改状态为2
                                                App.getDBcApplication().updateCalenderNoteDetailState("", listBean.id, UserId, 2, true);
                                            } else if (listBean.state == 2) { //异常
                                                App.getDBcApplication().updateCalenderNoteDetailState("", listBean.id, UserId, -1, true);
                                            }
                                        } else if (listBean.dataState == 3) {//删除
                                            if (listBean.state == 0) {//成功
                                                //删除该id的清单
                                                App.getDBcApplication().deleteCalenderNoteTitledetailData(listBean.id, UserId, true);
                                            } else if (listBean.state == 1) {//失败
                                                //更改状态为3
                                                App.getDBcApplication().updateCalenderNoteDetailState("", listBean.id, UserId, 3, true);
                                            } else if (listBean.state == 2) { //异常
                                                App.getDBcApplication().updateCalenderNoteDetailState("", listBean.id, UserId, -1, true);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    private void uploadNoteCode(final boolean isMinlocal) {
        JSONObject tDelListBeanListjsonObject = new JSONObject();
        JSONArray tDelListBeanListjsonArray = new JSONArray();
        //详情
        JSONObject listjsonObject = new JSONObject();
        JSONArray listjsonArray = new JSONArray();
        try {
            for (NoteTitleDetailBean.TDelListBean bean : tDelListBeanList) {
                JSONObject js = new JSONObject();
                js.put("id", bean.id);
                js.put("titleId", bean.titleId);
                js.put("uid", bean.uid);
                js.put("titles", bean.titles);
                js.put("imgPath", bean.imgPath);
                js.put("imgUrl", bean.imgUrl);
                js.put("shareUrl", bean.shareUrl);
                js.put("filed", bean.filed);
                js.put("nums", bean.nums);
                js.put("copys", bean.copys);
                js.put("styles", bean.styles);
                js.put("ltype", bean.ltype);
                js.put("orderId", bean.orderId);
                js.put("createTime", bean.createTime);
                js.put("changeTime", bean.changeTime);
                js.put("localTimes", bean.localTimes);
                js.put("remark", bean.remark);
                js.put("remark1", bean.remark1);
                js.put("remark2", bean.remark2);
                js.put("remark3", bean.remark3);
                js.put("remark4", bean.remark4);
                js.put("remark5", bean.remark5);
                js.put("remark6", bean.remark6);
                js.put("remark7", bean.remark7);
                js.put("remark8", bean.remark8);
                js.put("puid", bean.puid);
                js.put("pname", bean.pname);
                js.put("states", bean.states);
                js.put("sends", bean.sends);
                js.put("openState", bean.openState);
                js.put("updateState", bean.id < 0 ? 1 : App.getDBcApplication().getNoteTitleState(bean.titleId, UserId));
                tDelListBeanListjsonArray.put(js);
            }
            if (tDelListBeanList.size() > 0) {
                tDelListBeanListjsonObject.put("classTitle", tDelListBeanListjsonArray);
            } else {
                tDelListBeanListjsonObject = null;
            }

            for (NoteTitleDetailBean.ListBean bean : listBeanList) {
                JSONObject js = new JSONObject();
                js.put("id", bean.id);
                js.put("titleId", bean.titleId);
                js.put("uid", bean.uid);
                js.put("titles", bean.titles);
                js.put("imgPath", bean.imgPath);
                js.put("imgUrl", bean.imgUrl);
                js.put("shareUrl", bean.shareUrl);
                js.put("contents", bean.contents);
                js.put("nums", bean.nums);
                js.put("style", bean.style);
                js.put("endstate", bean.endstate);
                js.put("lType", bean.lType);
                js.put("orderId", bean.orderId);
                js.put("createTime", bean.createTime);
                js.put("changeTime", bean.changetime);
                js.put("cpath", bean.cpath);
                js.put("curl", bean.curl);
                js.put("remark", bean.remark);
                js.put("remark1", bean.remark1);
                js.put("remark2", bean.remark2);
                js.put("remark3", bean.remark3);
                js.put("remark4", bean.remark4);
                js.put("updateState", bean.id < 0 ? 1 : App.getDBcApplication().getNoteTitleDetailState(bean.titleId, UserId, bean.id));
                listjsonArray.put(js);
            }
            if (listBeanList.size() > 0) {
                listjsonObject.put("class1", listjsonArray);
            } else {
                listjsonObject = null;
            }

            HttpUtils httpUtils = new HttpUtils(5000);
            RequestParams params = new RequestParams();
            params.addBodyParameter("data", listjsonObject == null ? "" : listjsonObject.toString());
            params.addBodyParameter("dataTitle", tDelListBeanListjsonObject == null ? "" : tDelListBeanListjsonObject.toString());
            httpUtils.send(HttpMethod.POST, URLConstants.清单和清单标题上行, params,
                    new RequestCallBack<String>() {

                        @Override
                        public void onFailure(HttpException e, String msg) {
                            uploadNoteStatus(true, isMinlocal);
                            Log.e("TAG", "清单同步失败" + msg);
                        }

                        @Override
                        public void onSuccess(ResponseInfo<String> responseInfo) {
                            if (!TextUtils.isEmpty(responseInfo.result)) {
                                Gson gson = new Gson();
                                NoteService.noteSynBean bean = gson.fromJson(responseInfo.result, NoteService.noteSynBean.class);
                                if (bean.status == 0) {
                                    List<NoteService.noteSynBean.TDelListBean> tDeListBeanList = bean.tDelList;

                                    //标题同步没有异常
                                    List<Integer> errorList = new ArrayList<Integer>();

                                    for (NoteService.noteSynBean.TDelListBean tDeListBean : tDeListBeanList) {
                                        if (tDeListBean.dataState == 1) {//新增
                                            if (tDeListBean.state == 0) {//成功
                                                //修改id和状态值
                                                App.getDBcApplication().updateNoteTitleId(tDeListBean.id, tDeListBean.calendId, UserId);
                                                App.getDBcApplication().updateNoteTitleState(tDeListBean.calendId + "", UserId, 0, true);
                                            } else if (tDeListBean.state == 1) {//失败  可以不操作,但是避免意外
                                                //更改状态为1
                                                App.getDBcApplication().updateNoteTitleState(tDeListBean.id + "", UserId, 1, true);
                                            } else if (tDeListBean.state == 2) { //异常
                                                App.getDBcApplication().updateNoteTitleState(tDeListBean.id + "", UserId, -1, true);
                                                errorList.add(tDeListBean.id);
                                            }
                                        } else if (tDeListBean.dataState == 2) {//修改
                                            if (tDeListBean.state == 0) {//成功
                                                //修改该id状态值
                                                App.getDBcApplication().updateNoteTitleState(tDeListBean.id + "", UserId, 0, true);
                                            } else if (tDeListBean.state == 1) {//失败
                                                //更改状态为2
                                                App.getDBcApplication().updateNoteTitleState(tDeListBean.id + "", UserId, 2, true);
                                            } else if (tDeListBean.state == 2) { //异常
                                                App.getDBcApplication().updateNoteTitleState(tDeListBean.id + "", UserId, -1, true);
                                                errorList.add(tDeListBean.id);
                                            }
                                        } else if (tDeListBean.dataState == 3) {//删除
                                            if (tDeListBean.state == 0) {//成功
                                                //删除该id的清单
                                                App.getDBcApplication().deleteNoteTitleData(tDeListBean.id, UserId, true);
                                            } else if (tDeListBean.state == 1) {//失败
                                                //更改状态为3
                                                App.getDBcApplication().updateNoteTitleState(tDeListBean.id + "", UserId, 3, true);
                                            } else if (tDeListBean.state == 2) { //异常
                                                App.getDBcApplication().updateNoteTitleState(tDeListBean.id + "", UserId, -1, true);
                                                errorList.add(tDeListBean.id);
                                            }
                                        }
                                    }

                                    if (errorList.size() == 0) {
                                        List<NoteService.noteSynBean.ListBean> listBeans = bean.list;
                                        if (listBeans != null) {
                                            for (NoteService.noteSynBean.ListBean listBean : listBeans) {

                                                if (listBean.dataState == 1) {//新增
                                                    if (listBean.state == 0) {//成功
                                                        //修改id和状态值
                                                        App.getDBcApplication().updateNoteDetailId(listBean.id, listBean.calendId, UserId);
                                                        App.getDBcApplication().updateNoteTitleDetailState("", listBean.calendId, UserId, 0, true);
                                                    } else if (listBean.state == 1) {//失败  可以不操作,但是避免意外
                                                        //更改状态为1
                                                        App.getDBcApplication().updateNoteTitleDetailState("", listBean.id, UserId, 1, true);
                                                    } else if (listBean.state == 2) { //异常
                                                        App.getDBcApplication().updateNoteTitleDetailState("", listBean.id, UserId, -1, true);
                                                    }
                                                } else if (listBean.dataState == 2) {//修改
                                                    if (listBean.state == 0) {//成功
                                                        //修改该id状态值
                                                        App.getDBcApplication().updateNoteTitleDetailState("", listBean.id, UserId, 0, true);
                                                    } else if (listBean.state == 1) {//失败
                                                        //更改状态为2
                                                        App.getDBcApplication().updateNoteTitleDetailState("", listBean.id, UserId, 2, true);
                                                    } else if (listBean.state == 2) { //异常
                                                        App.getDBcApplication().updateNoteTitleDetailState("", listBean.id, UserId, -1, true);
                                                    }
                                                } else if (listBean.dataState == 3) {//删除
                                                    if (listBean.state == 0) {//成功
                                                        //删除该id的清单
                                                        App.getDBcApplication().deleteNoteTitledetailData(listBean.id, UserId, true);
                                                    } else if (listBean.state == 1) {//失败
                                                        //更改状态为3
                                                        App.getDBcApplication().updateNoteTitleDetailState("", listBean.id, UserId, 3, true);
                                                    } else if (listBean.state == 2) { //异常
                                                        App.getDBcApplication().updateNoteTitleDetailState("", listBean.id, UserId, -1, true);
                                                    }
                                                }
                                            }
                                        }
                                        sharedPrefUtil.putString(
                                                getApplication(),
                                                ShareFile.USERFILE,
                                                ShareFile.UPDATENOTESETTIME,
                                                bean.downTime.replace("T", " "));

                                    }
                                    uploadNoteStatus(false, isMinlocal);
                                } else {
                                    uploadNoteStatus(true, isMinlocal);
                                }
                            } else {
                                uploadNoteStatus(true, isMinlocal);
                            }
                        }
                    });
        } catch (JSONException e) {
            e.printStackTrace();
            uploadNoteStatus(true, isMinlocal);
        }
    }

    private void uploadGongKaiHongdian(List<NoteTitleDetailBean.TDelListBean> tDelListBeanList){
        for (NoteTitleDetailBean.TDelListBean bean: tDelListBeanList){
            if (bean.openState == 1){
                HttpUtils httpUtils = new HttpUtils(3000);
                RequestParams params = new RequestParams();
                params.addBodyParameter("titleId", bean.titleId);
                params.addBodyParameter("uId", bean.uid+"");
                params.addBodyParameter("cuId", UserId+"");
                httpUtils.send(HttpMethod.POST, URLConstants.清单留言公开方公开清单同步通知, params,
                        new RequestCallBack<String>() {

                            @Override
                            public void onFailure(HttpException e, String msg) {
                                android.util.Log.e("TAG", "清单同步失败" + msg);
                            }

                            @Override
                            public void onSuccess(ResponseInfo<String> responseInfo) {

                            }
                        });
            }
        }
    }

    /**
     * 上传清单数据状态
     */
    private void uploadNoteStatus(boolean isFailed, boolean isMinLocal) {
        if (!isMinLocal) {
            localSynServiceReciver = new Intent("localSynServiceReciver");
            localSynServiceReciver.putExtra("nflag", isFailed ? "nflag_false" : "nflag");
            sendBroadcast(localSynServiceReciver);
            uploadLocalFriendsStatus(false, false);
        } else {
            uploadLocalFriends(false);
        }
    }

    /**
     * 上传本地联系人
     */
    private void uploadLocalFriends(final boolean isMinlocal) {

        JSONObject jsonObject = new JSONObject();
        try {
            JSONArray jsonArray = new JSONArray();
            for (NewFriendsActivity.newFriendBean.PageBean.ItemsBean bean : itemsBeanList) {
                JSONObject object = new JSONObject();
                object.put("id", bean.id);
                object.put("puid", bean.puid);
                object.put("remark", bean.remark);
                object.put("remark1", bean.remark1);
                object.put("changeTime", bean.changeTime);
                object.put("createTime", bean.createTime);
                object.put("pname", bean.pname);
                object.put("pimgs", bean.pimgs);
                object.put("type", bean.type);
                object.put("updateState", App.getDBcApplication().getNewFriendUpdateState(bean.puid));
                object.put("uid", bean.uid);
                jsonArray.put(object);
            }
            jsonObject.put("says", jsonArray);
            if (itemsBeanList.size() > 0) {
                HttpUtils httpUtils = new HttpUtils(5000);
                RequestParams params = new RequestParams();
                params.addBodyParameter("data", jsonObject.toString());
                httpUtils.send(HttpMethod.POST, URLConstants.本地通讯录同步, params,
                        new RequestCallBack<String>() {

                            @Override
                            public void onFailure(HttpException e, String msg) {
                                uploadLocalFriendsStatus(true, isMinlocal);
                                Log.e("TAG", "本地通讯录同步失败" + msg);
                            }

                            @Override
                            public void onSuccess(ResponseInfo<String> responseInfo) {
                                if (!TextUtils.isEmpty(responseInfo.result)) {
                                    Gson gson = new Gson();
                                    NoteService.noteSynBean bean = gson.fromJson(responseInfo.result, NoteService.noteSynBean.class);
                                    if (bean.status == 0) {
                                        List<NoteService.noteSynBean.ListBean> listBeans = bean.list;
                                        if (listBeans != null) {
                                            for (NoteService.noteSynBean.ListBean listBean : listBeans) {

                                                if (listBean.dataState == 1) {//新增
                                                    if (listBean.state == 0) {//成功
                                                        //修改id和状态值
                                                        App.getDBcApplication().updateNewFriendId(listBean.id, listBean.calendId, UserId);
                                                        App.getDBcApplication().updateNewFriendState(listBean.calendId, UserId, 0);
                                                    } else if (listBean.state == 1) {//失败  可以不操作,但是避免意外
                                                        //更改状态为1
                                                        App.getDBcApplication().updateNewFriendState(listBean.id, UserId, 1);
                                                    } else if (listBean.state == 2) { //异常
                                                        App.getDBcApplication().updateNewFriendState(listBean.id, UserId, -1);
                                                    }
                                                } else if (listBean.dataState == 2) {//修改
                                                    if (listBean.state == 0) {//成功
                                                        //修改该id状态值
                                                        App.getDBcApplication().updateNewFriendState(listBean.id, UserId, 0);
                                                    } else if (listBean.state == 1) {//失败
                                                        //更改状态为2
                                                        App.getDBcApplication().updateNewFriendState(listBean.id, UserId, 2);
                                                    } else if (listBean.state == 2) { //异常
                                                        App.getDBcApplication().updateNewFriendState(listBean.id, UserId, -1);
                                                    }
                                                } else if (listBean.dataState == 3) {//删除
                                                    if (listBean.state == 0) {//成功
                                                        //删除该id的清单
                                                        App.getDBcApplication().deleteNewFriendData(listBean.id, UserId, false);
                                                    } else if (listBean.state == 1) {//失败
                                                        //更改状态为3
                                                        App.getDBcApplication().updateNewFriendState(listBean.id, UserId, 3);
                                                    } else if (listBean.state == 2) { //异常
                                                        App.getDBcApplication().updateNewFriendState(listBean.id, UserId, -1);
                                                    }
                                                }
                                            }
                                        }
                                        sharedPrefUtil.putString(
                                                getApplication(),
                                                ShareFile.USERFILE,
                                                ShareFile.UPDATENOTESETTIME,
                                                bean.downTime.replace("T", " "));
                                        Log.e("TAG", "本地通讯录同步成功");
                                        uploadLocalFriendsStatus(false, isMinlocal);
                                    } else {
                                        uploadLocalFriendsStatus(true, isMinlocal);
                                    }
                                } else {
                                    uploadLocalFriendsStatus(true, isMinlocal);
                                }
                            }
                        });
            }
        } catch (Exception e) {
            e.printStackTrace();
            uploadLocalFriendsStatus(true, isMinlocal);
        }

    }

    /**
     * 上传本地联系人数据状态
     */
    private void uploadLocalFriendsStatus(boolean isFailed, boolean isMinLocal) {
        if (!isMinLocal) {
            localSynServiceReciver = new Intent("localSynServiceReciver");
            localSynServiceReciver.putExtra("lflag", isFailed ? "lflag_false" : "lflag");
            sendBroadcast(localSynServiceReciver);
        }
        uploadPerson();
    }


    /**
     * 上传本地个人信息设置
     *
     * @param isFailed
     */
    private void uploadLocalPersonStatus(boolean isFailed) {
        localSynServiceReciver = new Intent("localSynServiceReciver");
        localSynServiceReciver.putExtra("pflag", isFailed ? "pflag_false" : "pflag");
        sendBroadcast(localSynServiceReciver);
    }

    /**
     * 上传个人设置
     */
    private void uploadPerson() {
        if (sharedPrefUtil.getString(getApplication(), ShareFile.USERFILE, ShareFile.USERID, "-1").equals("-1")) {
            String path = URLConstants.修改用户设置;
            final Map<String, String> pairs = new HashMap<String, String>();
            pairs.put("tbUserMannge.uid", sharedPrefUtil.getString(getApplicationContext(),
                    ShareFile.USERFILE, ShareFile.USERID, "0"));
            pairs.put("tbUserMannge.id", sharedPrefUtil.getString(getApplicationContext(),
                    ShareFile.USERFILE, ShareFile.SETID, "0"));
            pairs.put("tbUserMannge.openState", "0");
            pairs.put("tbUserMannge.ringCode", sharedPrefUtil.getString(getApplicationContext(),
                    ShareFile.USERFILE, ShareFile.MUSICCODE, "g_88"));
            pairs.put("tbUserMannge.ringDesc", sharedPrefUtil.getString(getApplicationContext(),
                    ShareFile.USERFILE, ShareFile.MUSICDESC, "完成任务"));
            pairs.put("tbUserMannge.beforeTime", sharedPrefUtil.getString(getApplicationContext(),
                    ShareFile.USERFILE, ShareFile.BEFORETIME, "0"));
            pairs.put("tbUserMannge.morningState", sharedPrefUtil.getString(getApplicationContext(),
                    ShareFile.USERFILE, ShareFile.MORNINGSTATE, "0"));
            pairs.put("tbUserMannge.morningTime", sharedPrefUtil.getString(getApplicationContext(),
                    ShareFile.USERFILE, ShareFile.MORNINGTIME, "07:58"));
            pairs.put("tbUserMannge.nightState", sharedPrefUtil.getString(getApplicationContext(),
                    ShareFile.USERFILE, ShareFile.NIGHTSTATE, "0"));
            pairs.put("tbUserMannge.nightTime", sharedPrefUtil.getString(getApplicationContext(),
                    ShareFile.USERFILE, ShareFile.NIGHTTIME, "18:30"));
            pairs.put("tbUserMannge.dayTime", sharedPrefUtil.getString(getApplicationContext(),
                    ShareFile.USERFILE, ShareFile.ALLTIME, "23:59"));
            pairs.put("tbUserMannge.dayState", sharedPrefUtil.getString(getApplicationContext(),
                    ShareFile.USERFILE, ShareFile.ALLSTATE, "0"));

            StringRequest request = new StringRequest(Method.POST, path,
                    new Response.Listener<String>() {

                        @Override
                        public void onResponse(String result) {
                            if (!TextUtils.isEmpty(result)) {
                                try {
                                    Gson gson = new Gson();
                                    SuccessOrFailBean bean = gson.fromJson(result,
                                            SuccessOrFailBean.class);
                                    if (bean.status == 0) {
                                        uploadLocalPersonStatus(false);
                                    } else {
                                        uploadLocalPersonStatus(true);
                                    }
                                } catch (JsonSyntaxException e) {
                                    e.printStackTrace();
                                    uploadLocalPersonStatus(true);
                                }
                            } else {
                                uploadLocalPersonStatus(true);
                            }
                        }
                    }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError volleyError) {
                    uploadLocalPersonStatus(true);
                }
            }) {
                @Override
                protected Map<String, String> getParams() throws AuthFailureError {
                    return pairs;
                }
            };
            request.setTag("postPerson");
            request.setRetryPolicy(new DefaultRetryPolicy(5000, 1, 1.0f));
            App.getHttpQueues().add(request);
        } else {
            uploadLocalPersonStatus(false);
        }
    }
}
